//
//  SDHomeHeaderView.m
//  skinDetection
//
//  Created by  liangzhun on 2018/10/30.
//  Copyright © 2018年  liangzhun. All rights reserved.
//

#import "SDHomeHeaderView.h"
#import "SDHomeProductList.h"
#import "SDProductDetailVC.h"
@interface SDHomeHeaderView ()<UIScrollViewDelegate>

@property (nonatomic ,strong)NSTimer *time;
@property (nonatomic ,strong)UIScrollView *scr;
@property (nonatomic ,strong)UIPageControl *pageControl;
@property (nonatomic ,strong)NSMutableArray *showListArray;
@property (nonatomic ,assign)int offset;
@end
@implementation SDHomeHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self layout];
        self.offset = 1;
        self.time = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    
    }
    
    return self;
}
- (void)changeImage
{
    
    self.offset ++;
    [self.scr setContentOffset:CGPointMake(self.offset * SDMainScreenWidth, 0) animated:YES];
  
}
- (void)layout
{
    [self.scr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.equalTo(self);
    }];
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(27);
    }];
    
}
- (void)setImageArray:(NSArray *)imageArray
{
    if (imageArray.count < 2)return;
    
    self.showListArray = [[NSMutableArray alloc] initWithArray:imageArray];
    [self.showListArray insertObject:imageArray.lastObject atIndex:0];
    [self.showListArray addObject:imageArray.firstObject];
    
    self.pageControl.numberOfPages = imageArray.count;
    self.pageControl.currentPage = 0;
    self.scr.contentSize = CGSizeMake(SDMainScreenWidth * self.showListArray.count, 135);
    self.scr.contentOffset = CGPointMake(SDMainScreenWidth, 0);
    for (int i = 0; i < self.showListArray.count; i++) {
        
        SDHomeProductList *list = self.showListArray[i];
        UIButton *imageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [imageBtn sd_setImageWithURL:[NSURL URLWithString:list.img] forState:UIControlStateNormal];
        [imageBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
//            SDProductDetailVC *vc = [SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDProductDetailVC class])];
//            vc.list = list;
//            [[SDMethodTool getCurrentVC].navigationController pushViewController:vc animated:YES];
        }];
        //[imageBtn setImage:[UIImage imageNamed:self.showListArray[i]] forState:UIControlStateNormal];
        [imageBtn setFrame:CGRectMake(i*SDMainScreenWidth, 0, SDMainScreenWidth, 135)];
        [self.scr addSubview:imageBtn];
        
    }
    
    
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int page = scrollView.contentOffset.x/SDMainScreenWidth;
    self.pageControl.currentPage = page == 0? self.showListArray.count - 2 - 1:page == self.showListArray.count - 1? 0: page - 1;
    if (self.showListArray.count - 1 == page) {
        
        self.offset = 1;
        [scrollView setContentOffset:CGPointMake(self.offset * SDMainScreenWidth, 0) animated:NO];
    }
  
    
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self.time setFireDate:[NSDate distantFuture]];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   
    int page = scrollView.contentOffset.x/SDMainScreenWidth;
    self.pageControl.currentPage = page == 0? self.showListArray.count - 2 - 1:page == self.showListArray.count - 1? 0: page - 1;
    if (page == 0) {
        
        [scrollView setContentOffset:CGPointMake((self.showListArray.count - 2) * SDMainScreenWidth, 0) animated:NO];
    }
    
    if (page == self.showListArray.count - 1) {
        [scrollView setContentOffset:CGPointMake(SDMainScreenWidth, 0) animated:NO];
    }
    
    [self.time setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
    self.offset = scrollView.contentOffset.x/SDMainScreenWidth;
}
#pragma mark - 懒加载
-(UIScrollView *)scr
{
    if (!_scr) {
        
        _scr = [[UIScrollView alloc] init];
        _scr.showsHorizontalScrollIndicator = NO;
        _scr.showsVerticalScrollIndicator = NO;
        _scr.bounces = NO;
        _scr.pagingEnabled = YES;
        _scr.delegate = self;
        [self addSubview:_scr];
        
    }
    
    return _scr;
    
}
-(UIPageControl *)pageControl
{
    if (!_pageControl) {
        
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.pageIndicatorTintColor = RGBA(255, 255, 255, 0.6);
        _pageControl.currentPageIndicatorTintColor = RGBA(255, 255, 255, 1);
        [self addSubview:_pageControl];
        
    }
    
    return _pageControl;
    
}
@end
